<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>缓冲运动</title>

    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        #box {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: red;
            top: 100px;
            left: 0px;
        }
    </style>
</head>

<body>

    <div id="box"></div>
    <button id="but">运动</button>
    <div style="width: 500px;height: 1000px;border-right: 1px solid black;"></div>
</body>
<script>
    var box = document.getElementById('box');
    var but = document.getElementById('but');
    var time = null;
    but.onclick = function() {
        Start(box, 500);
    }

    function Start(obj, targer) {
        clearInterval(time);
        time = setInterval(function() {
            var speed = (targer - obj.offsetLeft) / 8;
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
            document.title = (obj.offsetLeft);
            if (obj.offsetLeft == targer) {
                clearInterval(time);
            } else {
                obj.style.left = (obj.offsetLeft + speed) + 'px';
            }
        }, 18);
    }
</script>

</html>